@extends('layouts.app')

@section('content')
@if(session()->has('message'))
    <div class="alert alert-success" role="alert">{{ session()->get('message') }}</div>
@endif
<hr>
<form action="{{ route("feedback.store") }}" method="post">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="title">留言标题</label>
        <input type="text" class="form-control" name="title" id="title" placeholder="请输入您的留言标题">
    </div>
    <div class="form-group">
        <label for="content">留言内容</label>
        <textarea class="form-control" id="content" name="content" rows="5" placeholder="在这里输入您的留言内容"></textarea>
    </div>
    <hr>
    <button type="button" class="btn btn-default">提交</button>
</form>
@endsection
@section('scripts')
    <script>
        $(function () {
            $('button[type="button"]').click(function () {
                let token = $('input[name="_token"]').val();
                let title = $('#title').val();
                let content = $('#content').val();
                let route = $("form:eq(0)" ).attr( "action")

                $.ajax({
                    method: "POST",
                    url: route,
                    data: { title: title, content: content, _token: token },
                    success: function (res) {
                        if (res.status == 'error') {
                            alert(res.message);
                            return
                        } else {
                            alert('添加成功');
                            location.href = '/feedback';
                        }
                    }
                });
            });
        });
    </script>
@endsection
